<?php

class ProjectTopicHelper {
	/**
	 * @return null if no child, true if at least one is selected false otherwise
	 */
	public static function hasChildSelected($topic) {
		$r = null;

		if (isset($topic->children) && is_array($topic->children) && count($topic->children) > 0) {
			$r = false;
			foreach ($topic->children as $child) {
				$r = $r || $child->checked;
			}
		}

		return $r;
	}

	public static function adaptAllChildrenAndParentStatuses(&$topic) {
		if (nvl($topic->adviser_validation, ProjectTopic::NO) != ProjectTopic::YES) {
			if (nvl($topic->parent->adviser_validation, ProjectTopic::NO) == ProjectTopic::NO) {
				$topic->adviser_validation = ProjectTopic::NO;
			} else {
				$topic->adviser_validation = ProjectTopic::INHERITS;
			}
		}
		if (nvl($topic->owner_validation, ProjectTopic::NO) != ProjectTopic::YES) {
			if (nvl($topic->parent->owner_validation, ProjectTopic::NO) == ProjectTopic::NO) {
				$topic->owner_validation = ProjectTopic::NO;
			} else {
				$topic->owner_validation = ProjectTopic::INHERITS;
			}
		}
		if (nvl($topic->objective, ProjectTopic::NO) != ProjectTopic::YES) {
			if (nvl($topic->parent->objective, ProjectTopic::NO) == ProjectTopic::NO) {
				$topic->objective = ProjectTopic::NO;
			} else {
				$topic->objective = ProjectTopic::INHERITS;
			}
		}
	}


}
